// pages/add_addr/add_addr.js
let app = getApp();
let openid = app.userList.openid;
let jumpurl=app.jumpurl.url;
let modify_id;
let addr_id;

let addr_title
var username;
var tel;
var address1 = "";
var address2;
var jingwei = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   console.log("*******onload**********");
   console.log(options);
    console.log(app.userList.openid);
    var that = this;
    if(options){
      modify_id = options.id;
    }
    
    console.log("modify_id"+modify_id)
     //首先查询缓存 有缓存 说明从选择地址页面跳入 存入缓存数据  没有缓存说明是从地址列表页面跳转 调用接口读取原始数据
    if (wx.getStorageSync('username') == '') {
      username = '';
    } else {
      username = wx.getStorageSync('username');
    }
    if (wx.getStorageSync('tel') == '') {
      tel = '';
    } else {
      tel = wx.getStorageSync('tel');
    }
    if (wx.getStorageSync('address2') == '') {
      address2 = '';
    } else {
      address2 = wx.getStorageSync('address2');
    }
    if (wx.getStorageSync('addr_id') == '') {
      addr_id = modify_id;
      // wx.setStorageSync('addr_id', addr_id);
      // console.log("addr_id"+addr_id);
      // that.setData({
      //   addr_id: addr_id
      // })
    } else {
      addr_id = wx.getStorageSync('addr_id');
    }

    wx.getStorage({
      key: 'map_add_key',
      success: function (res) {
        address1 = res.data;
        that.setData({
          address1: address1
        })
      },
      fail: function (err) {
        console.log("获取地址" + err);
        address1 = '';
      }
    })
    wx.getStorage({
      key: 'title_key',
      success: function (res) {
        console.log("------------add_title--------------");
       
        addr_title = res.data;
        that.setData({
          addr_title: res.data
        })
      },
      fail: function (err) {
        console.log("获取地址" + err);
        
      }
    })

    

    wx.getStorage({
      key: 'jingwei_key',
      success: function (res) {
        jingwei = res.data;
        console.log("**************jingweidu缓存*************");
        console.log(jingwei);
        that.setData({
          jingwei: jingwei
        })
      },
      fail: function (err) {
        console.log("获取地址" + err);
        jingwei = '';
      }
    })
    if (!address1) {
      address1 = "";
    }
    if(!addr_title){
      addr_title="";
    }

    that.setData({
      username: username,
      tel: tel,
      address2: address2,
      addr_id:addr_id
    })

//没有缓存 调用接口
    if (username.length == 0 && tel.length == 0 && address1.length == 0 && address2.length == 0 ){
      console.log(app.userList.openid);
      ///请求接口 获取原地址信息
      wx.request({
        url: jumpurl + 'often/oldaddress',
        data: {
          openid: app.userList.openid,
          id: addr_id
        },
        success: function (res) {
          console.log("ok")
          console.log(res);
          var message = res.data.msg.message;
          username = message.name;
          tel = message.tel;
          addr_id = message.id;
          address1 = message.map_add;
          address2 = message.address;
          jingwei = message.jingwei;
          that.setData({
              username:username,
              tel:tel,
             
              address1:address1,
              address2:address2,
              jingwei:jingwei,
              addr_id:addr_id
          })
          wx.setStorageSync('addr_id', addr_id);
          wx.setStorageSync('username', username);
          wx.setStorageSync('tel', tel);
          wx.setStorageSync('address2', address2);
        },
        fail: function (err) {
          console.log("*********err******" + err);
        }
      })
    }

     

  },

  onShow:function(){
    this.onLoad();
  },

  /*************跳转页面 选择地址 */
  choseaddr: function (e) {
    var that = this;

    wx.setStorage({
      key: 'lujing',
      data: 'edit_addr/edit_addr',
    })

    wx.navigateTo({
      url: '/pages/address/address',
    })
   

  },

  
  username: function (e) {
    wx.setStorageSync('username', e.detail.value);
  },
  tel: function (e) {
    wx.setStorageSync('tel', e.detail.value);
  },
  address2: function (e) {
    wx.setStorageSync('address2', e.detail.value);
  },

/*************表单验证***************/
  formSubmit: function (e) {
    console.log(e);
    console.log(openid);
    var username = e.detail.value.username;
    var tel = e.detail.value.tel;
    var address1 = e.detail.value.address1;
    var address2 = e.detail.value.address2;
    var jingwei = e.detail.value.jingwei;
    var addr_id=e.detail.value.addr_id;
    if (username.length == 0) {
      wx.showToast({
        title: '请输入联系人',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (tel.length == 0) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    }

    if (tel.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1000
      })
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(tel)) {
      wx.showToast({
        title: '手机号格式有误！',
        icon: 'none',
        duration: 1000
      })
      return false;
    }

    if (address1.length == 0) {
      wx.showToast({
        title: '请选择地址',
        icon: "none",
        duration: 1000
      })
      return false;
    }

    if (address2.length == 0) {
      wx.showToast({
        title: '请输入详细地址',
        icon: "none",
        duration: 1000
      })
      return false;
    }

    if (username && tel && address1 && address2) {
      //请求接口
      wx.request({
        url: jumpurl + 'often/edit',
        data: {
          openid: app.userList.openid,
          name: username,
          tel: tel,
          map_add: address1,
          address: address2,
          jingwei: jingwei,
          id:addr_id

        },
        success: function (res) {
          console.log("****************表单提交成功****************");
          console.log(res);
          wx.removeStorage({
            key: 'map_add_key',
            success: function (res) {
              console.log("清除map_add成功");
            },
          })
          wx.removeStorage({
            key: 'jingwei_key',
            success: function (res) {
              console.log("清除jingwei成功");
            },
          })
          wx.removeStorageSync('username');
          wx.removeStorageSync("tel");
          wx.removeStorageSync("address2");
          wx.showToast({
            title: '提交成功',
            icon: "success",
            duration: 1000
          })
          setTimeout(function () {
            // wx.redirectTo({
            //   url: '/pages/addresslist/addresslist',
            // })
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        },
        fail: function (err) {
          console.log(err);
        },
        complete: function () {
          console.log("complete");
        }
      })
    }



  },
  
 



  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {
    wx.removeStorageSync('username');
    wx.removeStorageSync('tel');
    wx.removeStorageSync('address2');
    wx.clearStorage();
    wx.redirectTo({
      url: '/pages/addresslist/addresslist',
    })
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})